<template>
  <div class="app-container">
    <div class="title">试卷历史</div>
    <!-- 搜索框展示 -->
    <el-row type="flex" justify="space-between">
      <el-col :span="16"> <el-input class="search-box" type="text" prefix-icon="el-icon-search" size="small"
          placeholder="输入试卷名称搜索" v-model="queryParams.keyword" @keyup.enter.native="geExamPaperList">
        </el-input>
      </el-col>

      <el-col :span="8">
        <el-button class="col-item" size="mini" type="primary">
          <router-link :to="{ name: 'addExamPaper' }" class="link">
            添加试卷
          </router-link></el-button>
      </el-col>
    </el-row>

    <!-- 内容表格 -->
    <div class="content">
      <el-table :data="userList" @selection-change="handleSelectionChange" :row-class-name="tableRowClassName"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }">
        <el-table-column type="selection" width="55">
        </el-table-column>

        <el-table-column fixed label="操作" align="center" width="250">
          <template slot-scope="scope">
            <el-button type="text" size="small">
              <!-- 使用 v-permission 指令 -->
              <div class="link" @click="examPaperQuestion(scope.row.id)">查看</div>

            </el-button>
            <el-button type="text" size="small">
              <div class="link" @click="updateExamPaper(scope.row.id)">编辑</div>
            </el-button>
            <el-button type="text" size="small">
              <!-- 使用 v-permission 指令 -->
              <router-link :to="{ name: 'questions', params: { id: scope.row } }" class="link">
                成绩
              </router-link>
            </el-button>
            <el-button type="text" size="small">
              <div class="link" @click="delExamPaper(scope.row)">删除</div>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column fixed prop="name" label="考试名称" align="center" width="300">
        </el-table-column>
        <el-table-column fixed label="总分" align="center" width="150">
          <template slot-scope="{row}">
            {{ row.question_num * row.question_score }}
          </template>
        </el-table-column>
        <el-table-column label="考试时长" align="center" width="150">
          <template slot-scope="{row}">
            {{ row.exam_time }}分钟
          </template>
        </el-table-column>
        <el-table-column label="题数" align="center" width="150">
          <template slot-scope="{row}">
            {{ row.question_num }}题
          </template>
        </el-table-column>
        <el-table-column prop="remake" label="考试人数" align="center" width="150">
        </el-table-column>
        <el-table-column prop="nick_name" label="创建人" align="center" width="150">
        </el-table-column>
        <el-table-column prop="createtime" label="创建时间" align="center" width="200">
        </el-table-column>
        <el-table-column>
          <!-- 占空位 -->
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination @size-change="changePageSize" @current-change="changePageIndex"
        :current-page="queryParams.pageIndex" :page-sizes="[10, 20, 50]" :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="userTotal">
      </el-pagination>
    </div>

  </div>
</template>
<script>


import { geExamPaperList, delExamPaper } from '@/api/examPaper'
import { get } from 'lodash';

export default {
  name: 'examPaper',
  created() {
    //访问后端接口
    this.geExamPaperList()
    // 初始化防抖函数

  },
  watch: {
    //监听变化
    sortType(newVal) {

      this.queryParams.sortType = newVal;
      this.geExamPaperList()
    },

  },
  data() {
    return {
      userId: null,
      //批量选中
      selectedRows: [],
      // 用户总记录数
      userTotal: 0,
      userList: [],
      // 查询参数
      queryParams: {
        sortType: '3',
        pageIndex: 0,
        pageSize: 10,
        name: '',
        // phone: '',
        keyword: ''
      },
    }
  },
  methods: {
    updateExamPaper(id) {
      this.$router.push({
        name: 'addExamPaper',
        params: {
          id: id // 如果是编辑就传，新增就不传
        }
      });
    },
    examPaperQuestion(id) {
      this.$router.push({
        name: 'examQuestions',
        params: {
          id: id // 如果是编辑就传，新增就不传
        }
      });
    },
    async delExamPaper(row) {
      this.$confirm('删除后，可在【 回收站 】恢复', '确定要删除该试卷吗？', {
        type: 'warning'
      })
        .then(async () => {
          await delExamPaper({ id: row.id });
          this.$message.success('删除成功');
          this.geExamPaperList();
        })
        .catch(action => {
          if (action === 'cancel') {
            console.log('用户点击了取消');
          } else {
            console.error('删除失败:', action);
          }
        });
    },
    async geExamPaperList() {
      const { data, total } = await geExamPaperList(this.queryParams)

      this.userList = data
      this.userTotal = total
    },
    // 监听表格选中变化
    handleSelectionChange(selection) {
      this.selectedRows = selection.map(row => row.id);

    },
    tableRowClassName({ row, rowIndex }) {
      // 判断是否是偶数行（索引从 0 开始）
      if (rowIndex % 2 === 1) {
        return 'even-row'; // 偶数行的类名
      }
      return ''; // 其他行不添加类名
    },
    changePageIndex(newPageIndex) {
      //切换页数
      this.queryParams.pageIndex = newPageIndex - 1
      this.geExamPaperList()
    },
    changePageSize(newPageSize) {
      //切换条数
      this.queryParams.pageSize = newPageSize
      //切换条数 页数默认置0
      this.queryParams.pageIndex = 0
      this.geExamPaperList()
    },

    reset() {
      this.queryParams = {
        sortType: '3',
        pageIndex: 0,
        pageSize: 10,
        name: '',
        // phone: '',
        keyword: ''
      }
    },

  }

}
</script>
<style scoped>
.app-container {
  margin: 10px;
  background: #fff;
  border-radius: 5px;
  padding: 10px;

  .title {
    display: block;
    height: 40px;
    line-height: 40px;
    font-size: 16px;
    /* 文字居中对齐 */
    margin-bottom: 10px;
    /* 给标题下方留出间距 */
    border-bottom: 1px solid #e8eaec;
  }

  .search-box {
    width: 280px;
    /* 设置宽度 */
    height: 20px;
    /* 设置高度 */
    line-height: 20px;
    margin-bottom: 15px;
    margin-right: 10px;
    /* 确保输入框内容垂直居中 */
    font-size: 12px;
    /* 调整字体大小以适应高度 */
  }

  .button-box {
    /* 设置宽度 */
    height: 32px;
    /* 设置高度 */
    line-height: 20px;
    font-size: 12px;

  }

  .col-item {
    margin-left: 10px;
  }

  .search {
    margin-left: 10px;
    margin-top: 20px;
    margin-bottom: 20px;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 20px;
  }

  /* 每个搜索组 */
  .search-group {
    display: flex;
    align-items: center;
    /* 垂直对齐 */
    width: calc(20% - 10px);
    margin-right: 30px;
    /* 每行放两个 */
  }

  .search-label {
    font-size: 12px;
    margin-top: 10px;
    color: #606266;
    /* 防止文字换行 */
  }

  .search-item {
    flex: 1;
    /* 输入框占据剩余空间 */
    height: 20px;
    line-height: 20px;
    font-size: 12px;

  }

  /* 按钮组样式 */
  .action-buttons {
    display: flex;
    margin-top: 10px;
    align-items: center;
    gap: 5px;
    /* 按钮之间的间距 */
    width: auto;
    /* 自适应宽度 */
  }

  .content {
    margin-top: 20px;

    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display: inline-block;
    }
  }

  .pagination {
    margin-top: 20px;
  }




}
</style>
